<?php

/**
 * Register our sidebars and widgetized areas.
 *
 */
function get_cat_slug($the_cate_id) {
    global $wpdb;
    $sql = "select * from wp_terms where name='" . $the_cate_id . "'";
    $myrows = $wpdb->get_row($sql);
    if ($myrows != null) {
        return $myrows->slug;
    }
    return 'khong co';
}

function main_Menu($parent_id = '0') {
    global $wpdb;
    $sql = "SELECT * FROM `wp_term_taxonomy` join wp_terms on wp_term_taxonomy.term_id =wp_terms.term_id and taxonomy='wpsc_product_category' and parent='$parent_id'";
    $myrows = $wpdb->get_results($sql);
    return $myrows;
}

function sub_Menu($parent_id) {
    global $wpdb;
    $sql = "SELECT * FROM `wp_term_taxonomy` join wp_terms on wp_term_taxonomy.term_id =wp_terms.term_id and taxonomy='wpsc_product_category' and parent='$parent_id'";
    $myrows = $wpdb->query($sql);
    return $myrows;
}

function category_Image($cate_id) {
    global $wpdb;
    $sql = "select * from wp_wpsc_meta where meta_key='image' and object_type='wpsc_category' and object_id='$cate_id'";
    $myrows = $wpdb->get_results($sql);
    if (is_array($myrows) && isset($myrows[0])) {
        echo get_template_directory_uri() . "/" . $myrows[0]->meta_value;
    }
}

function send_Info_Buy() {
    if (isset($_POST['buyerName']))
        $name = $_POST['buyerName'];
    if (isset($_POST['buyerPhone']))
        $phone = $_POST['buyerPhone'];
    if (isset($_POST['buyerEmail']))
        $email = $_POST['buyerEmail'];
    if (isset($_POST['buyerAddress']))
        $address = $_POST['buyerAddress'];
    if (isset($_POST['buyerDistrictId']))
        $distric = $_POST['buyerDistrictId'];
    if (isset($_POST['buyerCityId']))
        $city = $_POST['buyerCityId'];
    if (isset($_POST['itemUrl']))
        $url = $_POST['itemUrl'];
    if (isset($_POST['itemId']))
        $item = $_POST['itemId'];
    if (isset($_POST['quantity']))
        $quantity = $_POST['quantity'];
    if (isset($_POST['note']))
        $note = $_POST['note'];
    $mesage = "
            -Tên : $name,
            - So DT: $phone,
            - Email: $email,
            - Địa chỉ: $address,
            - Tỉnh/Thành: $city,
            - Số lượng: $quantity,
            - Ghi chú: $note.
            ";
    wp_mail('lamduykhanhit@gmail.com', 'Đặt mua hàng', $mesage);
    wp_redirect(get_page_link(20));
    exit;
}

if (isset($_POST['buy_submit']) && $_POST['buy_submit'] == 1) {
    send_Info_Buy();
}

function get_name($post_id) {
    global $wpdb;
    $sql = "select * from wp_posts where id='$post_id'";
    $myrows = $wpdb->get_results($sql);
    if (is_array($myrows) && isset($myrows)) {
        $name = $myrows[0];
        return $name->post_title;
    }
}

function get_List_Product_Image($product_id) {
    global $wpdb;
    //$post_thumbnail = get_post_thumbnail_id();//read the thumbnail id
    $attachments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE post_parent = $product_id AND post_type = 'attachment'", $product_id));
    return $attachments;
}

function my_product_with_categoty($id_product_current, $catarogy_current_id_array) {
    $category_slug = $catarogy_current_id_array->slug;
    $args = array(
        'posts_per_page' => 4,
        'post_type' => 'wpsc-product',
        'wpsc_product_category' => $category_slug);
    $myposts = get_posts($args);
    return $myposts;
}

require( get_template_directory() . '/inc/widgets.php' );

function twentyeleven_widgets_init() {

    register_widget('Twenty_Eleven_Ephemera_Widget');

    register_sidebar(array(
        'name' => __('Main Sidebar', 'twentyeleven'),
        'id' => 'sidebar-1',
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => "</aside>",
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ));

    register_sidebar(array(
        'name' => __('Showcase Sidebar', 'twentyeleven'),
        'id' => 'sidebar-2',
        'description' => __('The sidebar for the optional Showcase Template', 'twentyeleven'),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => "</aside>",
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ));

    register_sidebar(array(
        'name' => __('Footer Area One', 'twentyeleven'),
        'id' => 'sidebar-3',
        'description' => __('An optional widget area for your site footer', 'twentyeleven'),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => "</aside>",
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ));

    register_sidebar(array(
        'name' => __('Footer Area Two', 'twentyeleven'),
        'id' => 'sidebar-4',
        'description' => __('An optional widget area for your site footer', 'twentyeleven'),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => "</aside>",
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ));

    register_sidebar(array(
        'name' => __('Footer Area Three', 'twentyeleven'),
        'id' => 'sidebar-5',
        'description' => __('An optional widget area for your site footer', 'twentyeleven'),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => "</aside>",
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ));
}

add_action('widgets_init', 'twentyeleven_widgets_init');

// functions get image for category
function get_Image_Category($cate_id) {
    global $wpdb;
    //$post_thumbnail = get_post_thumbnail_id();//read the thumbnail id
    $attachments = $wpdb->get_results($wpdb->prepare("SELECT * FROM wp_wpsc_meta WHERE object_id='$cate_id' and meta_key='image'"));
    if (is_array($attachments) && isset($attachments[0])) {
        return $attachments[0];
    }
}

function send_Buy_Product() {

    $name = $_POST['name'];
    $phone = $_POST['phone'];
    $email = $_POST['email'];
    $address = $_POST['address'];
    $product_name = $_POST['prodcut_name'];
    $mesage = "Thông tin đặt hàng:
            Tên người mua: $name
            Điện thoại: $phone
            Email: $email
            Địa chỉ: $address
            Tên sản phẩm: $product_name";
    wp_mail('lamduykhanhit@gmail.com', 'Đặt mua hàng', $mesage);
    wp_redirect(get_page_link(20));
    exit;
}

if (isset($_POST['send_buy']) && $_POST['send_buy'] == "send_buy") {
    send_Buy_Product();
}

add_action('admin_menu', 'register_my_custom_submenu_page');

function register_my_custom_submenu_page() {
    //  add_submenu_page('tools.php', 'Thống kê', 'Thống Kê', 'manage_options', 'my-custom-submenu-page', 'my_custom_submenu_page_callback');

    add_menu_page('Thống kê', 'Thống Kê', 'edit_others_posts', 'my-custom-submenu-page', 'my_custom_submenu_page_callback');
}

function my_custom_submenu_page_callback() {
    global $wpdb;
    $month_visit = $wpdb->get_results('select * from wp_vs_month');
    $week_munvs = date('W');
    if ($week_munvs <= 7)
        $week_munvs = 7;
    $week_munvs = $week_munvs - 7;
    $week_visit = $wpdb->get_results("select * from wp_vs_week limit $week_munvs,7");

    $days_visit = $wpdb->get_results("select * from wp_vs_days order by id desc limit 0,7");
    $days_visit_checkloan = $wpdb->get_results("select * from wp_vs_checkloan order by id desc limit 0,7");

    $nam_hien_tai = date('Y');
    $length_year = ($nam_hien_tai - 2013) + 3;
    $year_visit = $wpdb->get_results("select * from wp_vs_year order by id asc limit 0,$length_year");
    ?>
    <!doctype html>
    <html>
        <head>
            <meta charset="utf-8">
            <title>Untitled Document</title>
        </head>
        <style>
            body {
                margin: 0;
                padding: 0;
                line-height: 2.5em;
                font-family: arial, "Times New Roman", Times, san-serif;

                font-size: 13px;
                color: #666;
                background: #ffffff;
            }
            #wrapper {
                margin: 0 auto;
                width:980px;
                background: #ffffff;	
            }
            div{
                margin:0 0 15px 0;
            }
            table.table {
                border-collapse: collapse;
                border-spacing: 0;
                font-size: 0.85em;
            }
            table.table th, td {
                height: 50px;
                width: 150px;
            }
            table.table th, table.table td {
                border-color: #DDDDDD;
                border: 1px solid #D4D4D4 !important;
                margin: 0;
            }
            table.table th {
                background-color: #FF6600;
                color: #666666;
                text-align: center;

                color: #FFFFFF !important;
            }
        </style>
        <body>
            <div id="wrapper">
                <h2>Thống kê ngày</h2>
                <div>
                    <table class="table">
                        <tr>
                            <th>+</th>
                            <?php
                            $day_show = '';
                            foreach ($days_visit as $days_vs) {
                                ?>
                                <?php
                                $day_show = "<th><b>" . $days_vs->date_current . "</b></th>" . $day_show;
                                ?>
                            <?php }echo $day_show; ?>
                        </tr>
                        <tr>
                            <th>Lượt xem </th>
                            <?php
                            $day_show = '';
                            foreach ($days_visit as $days_vs) {
                                ?>
                                <?php $day_show = "<td style='font-size: 16px'>" . $days_vs->visits . "</td>" . $day_show; ?>
                            <?php }echo $day_show; ?>
                        </tr>
                    </table>
                </div>
                <h2>Thống kê tuần</h2>
                <div>
                    <table class="table">
                        <th>+</th>
                        <?php foreach ($week_visit as $week_vs) { ?>
                            <th><b>Tuần Thứ <?php echo $week_vs->current_week; ?></b></th>
                        <?php } ?>
                        <tr>
                            <th>Lượt xem</th>
                            <?php foreach ($week_visit as $week_vs) { ?>
                                <td style="font-size: 16px"><?php echo $week_vs->visits; ?></td>
                            <?php } ?>
                        </tr>
                    </table>
                </div>
                <h2>Thống kê số lượng truy cập theo tháng</h2>
                <div>
                    <table class="table">
                        <th><b>Năm <?php echo date('Y'); ?></b></th>
                        <th>Tháng 1</th>
                        <th>Tháng 2</th>
                        <th>Tháng 3</th>
                        <th>Tháng 4</th>
                        <th>Tháng 5</th>
                        <th>Tháng 6</th>
                        <th>Tháng 7</th>
                        <th>Tháng 8</th>
                        <th>Tháng 9</th>
                        <th>Tháng 10</th>
                        <th>Tháng 11</th>
                        <th>Tháng 12</th>
                        <tr>
                            <th>Lượt xem</th>
                            <?php foreach ($month_visit as $month) { ?>
                                <td style="font-size: 16px"><?php echo $month->visits; ?></td>
                            <?php } ?>
                        </tr>
                    </table>
                </div>
                <h2>Thống kê số lượng truy cập theo Năm</h2>
                <div>
                    <table class="table">
                        <th><b>+</b></th>
                        <?php foreach ($year_visit as $year) { ?>
                            <th>Năm <?php echo $year->current_year; ?></th>
                        <?php } ?>
                        <tr>
                            <th>Lượt xem</th>
                            <?php foreach ($year_visit as $month) { ?>
                                <td style="font-size: 16px"><?php echo $month->visits; ?></td>
                            <?php } ?>
                        </tr>

                    </table>
                </div>

                <h2>Thống kê số lượng kiểm tra tiền phải đóng hàng tháng</h2>
                <div>
                    <table class="table">
                        <tr>
                            <th>+</th>
                            <?php
                            $day_show = '';
                            foreach ($days_visit_checkloan as $days_vs) {
                                $day_show = "<th><b>" . $days_vs->date_current . "</b></th>" . $day_show;
                            }
                            echo $day_show;
                            ?>
                        </tr>
                        <tr>
                            <th>Số lần</th>
                            <?php
                            $day_show = '';
                            foreach ($days_visit_checkloan as $days_vs) {
                                $day_show = "<td style='font-size: 16px'>" . $days_vs->visits . "</td>" . $day_show;
                            }
                            echo $day_show;
                            ?>
                        </tr>

                    </table>
                    </br>
                    <a href="<?php echo get_page_link(181); ?>">Export data</a>
                </div>
            </div>
        </body>
    </html>
<?php }


add_theme_support( 'post-thumbnails' ); 

?>